import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/enum/login_status.dart';
import 'package:love_app/init/main_init.dart';
import 'package:love_app/init/socket_init.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/routes/application.dart';
import 'package:love_app/routes/routes.dart';
import 'package:provider/provider.dart';

// 广告页面
class AdPage extends StatefulWidget {
  @override
  _AdPageState createState() => _AdPageState();
}

class _AdPageState extends State<AdPage> {
  int _timeOut; // 启动页秒数
  Timer _timer;

  @override
  void initState() {
    super.initState();
    _timeOut = 5;
    _startTimeOut();
  }

  @override
  void dispose() {
    super.dispose();
    _timeOut = 0;
    if (_timer != null) {
      // 如果有实例化就取消 
     _timer.cancel();
   }
  }

  @override
  Widget build(BuildContext context) {
    _screenSetting(context);
    return Container(
      height: Su().h(1334),
      width: Su().w(750),
      alignment: Alignment.topRight,
      decoration: BoxDecoration(
        image: DecorationImage(
          image: AssetImage('images/ad_page_img/ad.png'),
          fit: BoxFit.fill,
        ),
      ),
      child: _cricularBorder(),
    );
  }

  // 注册适应屏幕插件
  void _screenSetting(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
  }

  //  圆形倒计时
  Widget _cricularBorder() {
    return Container(
      width: Su().w(100),
      height: Su().w(100),
      margin: EdgeInsets.only(
        right: Su().w(50),
        top: Su().h(50),
      ),
      alignment: Alignment.center,
      decoration: BoxDecoration(
          color: Color.fromRGBO(255, 255, 255, 0),
          borderRadius: BorderRadius.circular(Su().w(90)),
          border: Border.all(
            color: Colors.white,
            width: 1,
          )),
      child: _timeNum(),
    );
  }

  // 秒数倒计时, 点击则跳到首页
  Widget _timeNum() {
    return FlatButton(
      child: Text(
        _timeOut.toString(),
        style: Su().textStyle(Colors.black, 30),
      ),
      onPressed: () {
        goToHomePage();
      },
    );
  }

  // 初始化启动倒计时
  void _startTimeOut() {
    final callback = (timer) {
      if(_timeOut > 0){
        _timeOut --;
        setState(() {});
      }else{
        _timer.cancel();
        goToHomePage();
      }
    };
    // 周期性的倒计时
    _timer = Timer.periodic(Duration(seconds: 1), callback);
  }

  /// 启动页结束后要跳转的页面
  Future<void> goToHomePage()async{
    // 初始化全局共享model
    _timer.cancel();
    LoginStatus loginStatus = await MainInit.getLoginStatus(context: context);
    if(loginStatus == LoginStatus.login){
      ShareProvider shareProvider = Provider.of<ShareProvider>(context, listen: false);
      // 开始初始化socket
      SocketInit.socketInit(context, shareProvider.token, shareProvider.uid);
      Application.router.navigateTo(context, Routes.oldBottomBar, clearStack: true);
    }else{
      Application.router.navigateTo(context, "/loginPage", clearStack: true);
    }
  }
}
